#ifndef _TimerManager_h_
#define _TimerManager_h_


#include <stdint.h>
#include <boost/function.hpp>
#include <boost/asio.hpp>
#include "boost/asio/deadline_timer.hpp"
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include "boost/thread/recursive_mutex.hpp"

#include "IObject/ITimerManager.h"

class TimerManager : public ITimerManager
{
	typedef boost::recursive_mutex::scoped_lock scoped_lock;
    struct timer_data
    {
    public:
		timer_data(boost::asio::io_service &io_timer) : m_timer(io_timer) { m_timer_id = 0; m_duration = 0; }
    public:
        uint32_t					m_timer_id;
		boost::asio::deadline_timer m_timer;
        uint32_t					m_duration;
        OnTimer						m_handler;
    };

public:
	TimerManager(boost::asio::io_service * pIO);
    virtual ~TimerManager();

public:
	TimerID schedule(TimerID tCurID, uint32_t duration, const OnTimer & handler);
    void cancel(TimerID tid);
    void stop();

private:
    void on_timer(const boost::system::error_code &error, timer_data * data);
    void delete_timer(TimerID id);
    void add_timer(TimerID id, uint32_t duration, const OnTimer & handler);

private:
    boost::asio::io_service				*m_pIOService;
    boost::recursive_mutex				m_mutex;
	TimerID								m_global_timer_id;
    std::map<uint32_t, timer_data*>     m_timer_map;
};

#endif
